package zyy.control;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import zyy.model.BeanProduct_Dir;
import zyy.util.BusinessException;
import zyy.util.DBUtil;
import zyy.util.DbException;

public class Product_DirManager {
  public List<BeanProduct_Dir> loadByMID(int MID) throws DbException {
    List<BeanProduct_Dir> result = new ArrayList<BeanProduct_Dir>();
    Connection conn = null;
    try {
      conn = DBUtil.getConnection();
      String sql = "SELECT pdOrder,pdName,pdCnt,productDirID FROM Product_Dir WHERE MID=?";
      java.sql.PreparedStatement pst = conn.prepareStatement(sql);
      pst.setInt(1, MID);
      java.sql.ResultSet rs = pst.executeQuery();
      while (rs.next()) {
        BeanProduct_Dir p = new BeanProduct_Dir();
        p.setMID(MID);
        p.setPdOrder(rs.getInt(1));
        p.setPdName(rs.getString(2));
        p.setPdCnt(rs.getInt(3));
        p.setProductDirID(rs.getInt(4));
        result.add(p);
      }
      rs.close();
      pst.close();
    } catch (SQLException e) {
      e.printStackTrace();
      throw new DbException(e);
    } finally {
      if (conn != null)
        try {
          conn.close();
        } catch (SQLException e) {
          e.printStackTrace();
        }
    }
    return result;
  }

  public void add(String pdName, int MID) throws BusinessException {
    // 需要先查出原有的最大 pdOrder
    int pdOrder;
    System.out.printf("新建分类：%s   宿主商家ID：%d", pdName, MID);
    Connection conn = null;
    try {
      conn = DBUtil.getConnection();
      String sql = " SELECT pdOrder FROM Product_Dir WHERE MID = ? ORDER BY pdOrder DESC LIMIT 0, 1";
      java.sql.PreparedStatement pst = conn.prepareStatement(sql);
      pst.setInt(1, MID);
      java.sql.ResultSet rs = pst.executeQuery();
      if (rs.next()) {
        pdOrder = rs.getInt(1) + 1;
      } else {
        pdOrder = 1;
      }
      sql = "INSERT INTO Product_Dir(pdName, MID, pdCnt, pdOrder)values(?,?,0,?)";
      pst = conn.prepareStatement(sql);
      pst.setString(1, pdName);
      pst.setInt(2, MID);
      pst.setInt(3, pdOrder);
      pst.executeUpdate();
      rs.close();
      pst.close();
    } catch (SQLException e) {
      e.printStackTrace();
      throw new BusinessException(e.getMessage());
    } finally {
      if (conn != null)
        try {
          conn.close();
        } catch (SQLException e) {
          e.printStackTrace();
        }
    }
  }

  public void unadd(int pdID) throws BusinessException {
    // 如果有对应商品（pdCnt不为0则不允许删除）
    // pdCnt不为0已在按钮触发事件中处理
    Connection conn = null;
    try {
      conn = DBUtil.getConnection();
      String sql = "DELETE FROM Product_Dir WHERE productDirID=? AND pdCnt<=0";
      java.sql.PreparedStatement pst = conn.prepareStatement(sql);
      pst.setInt(1, pdID);
      if (pst.executeUpdate() == 0) {
        throw new BusinessException("该商品分类内仍有商品，不可删除");
      }
      pst.close();
    } catch (SQLException e) {
      e.printStackTrace();
      throw new BusinessException(e.getMessage());
    } finally {
      if (conn != null)
        try {
          conn.close();
        } catch (SQLException e) {
          e.printStackTrace();
        }
    }
  }
}